/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 16, 2011 6:19:43 PM
 */
package carrello.impl.boundary.administrator;

import carrello.exception.CarrelloException;
import carrello.impl.boundary.AbstractUI;
import carrello.impl.controller.administrator.AddCategoryCTRL;
import carrello.model.entities.*;
import carrello.model.entitydao.CategoryDAO;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import static carrello.util.EntityManagerUtil.getEntityManager;


public class AddCategoryUI extends AbstractUI{
    Logger log = Logger.getLogger(AddCategoryUI.class);
    private List<Category> parent;
    private String categoryName;
    private String description;
    private String[] required;
    private String pname;
    private String[] aname;
    private String[] atype;
    private String[] inherit;
    private String error;

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
    
    public String[] getAname() {
        return aname;
    }

    public void setAname(String[] aname) {
        this.aname = aname;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String[] getRequired() {
        return required;
    }

    public void setRequired(String[] required) {
        this.required = required;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public List<Category> getParent() {
        return new CategoryDAO().getAll();
        
    }

    public void setParent(List<Category> parentCategories) {
        this.setParent(parentCategories);
    }

    public String[] getAtype() {
        return atype;
    }

    public void setAtype(String[] atype) {
        this.atype = atype;
    }

    public String[] getInherit() {
        return inherit;
    }

    public void setInherit(String[] inherit) {
        this.inherit = inherit;
    }
    
    
    @Override
    public String execute() 
    {
        getEntityManager().getHibernateSession().clear();
        if ( !getSessionIsLoggedIn()) {
            return LOGIN;
        } // if
        List<String[]> tupleList = new ArrayList<String[]>();
        String[] attributeNames = getAname();
        String[] attributeType = getAtype();
        String[] require       = getRequired();
        String[] chbox = null;
        if (categoryName == null) {
            return INPUT;
        } else if (attributeNames == null){
            if (new CategoryDAO().getByName(categoryName) != null) {
                setError("Category already exist, Please select a different name");
                return ERROR;
            } else if (categoryName.trim().equals("")) {
                setError("Please provide a Category Name");
                return ERROR;
            } 
            
            AddCategoryCTRL add = new AddCategoryCTRL();
            add.addCategory(pname, categoryName, description, inherit);
            return SUCCESS;
        } else {
            if (new CategoryDAO().getByName(categoryName) != null) {
                setError("Category already exist, Please select a different name");
                return ERROR;
            } else if (categoryName.trim().equals("")) {
                setError("Please provide a Category Name");
                return ERROR;
            } // if
            try {
                chbox = new String[attributeNames.length];
                int j = 0;
                for (int i = 0; i < require.length; i++) {
                    if ((require[i].equals("checked"))) {
                        chbox[j] = require[i];
                        j++;
                        i++;
                    } else {
                        chbox[j] = require[i];
                        j++;
                    } // if
                } // for
                tupleList = getAttributeTuples(attributeNames, attributeType, chbox);
                AddCategoryCTRL add = new AddCategoryCTRL();
                add.addCategory(pname, categoryName, description, tupleList, inherit);
            } catch (CarrelloException cex) {
                log.info("AddCategoryUI exception: " + cex);
                setError(cex.getMessage());
                return ERROR;
            } // try
            return SUCCESS;
        } // if
    } // execute
    
    /**
     * Creates a list (resultset) of Attribute for inserting in the attribute table
     * @param name     The attribute name
     * @param type     The attribute value
     * @param required The required field
     * @return         List containing the tuples of attribute
     */
    private List<String[]> getAttributeTuples(String[] name, String[] type, String[] check)
    {
        List<String[]> tupleList = new ArrayList<String[]>();
        String[] tuple = null;
        for (int i = 0; i < name.length; i ++) {
            tuple = new String[3];
            tuple[0] = name[i];
            tuple[1] = type[i];
            tuple[2] = check[i];  
            tupleList.add(tuple);
        }
        return tupleList;
    }
//    public static void main(String [] args)
//    {
//        
//        new AddCategoryUI().getParentList().size();
//    }
}
